package com.jumi.microservice.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ExportExcelCommonResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.dto.admin.WalletAccountRequest;
import com.jumi.microservice.dto.admin.WalletAccountResponse;
import com.jumi.microservice.dto.export.WalletAccountExport;
import com.jumi.microservice.service.admin.WalletAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author mazhao
 * @Date 2020-07-07 18:16
 */
@Api(tags = {"后台资金账户接口"})
@RestController
@RequestMapping("admin/wallet")
public class AdminWalletAccountController extends BaseController {

    @Resource
    private WalletAccountService walletAccountService;

    @PostMapping("/accountList")
    @ApiOperation("资金账户列表")
    public WalletAccountResponse WalletAccount(WalletAccountRequest request) {
        return walletAccountService.accountList(request);
    }

    @ApiOperation("资金账户导出")
    @PostMapping("/export")
    public void excelExport(HttpServletResponse response, WalletAccountRequest request) throws IOException {
        request.checkRequest();
        if(request.getMemberLevel()==null && (request.getPhoneNum()==null||request.getPhoneNum().trim().isEmpty())){
            return ;
        }
        ExportExcelCommonResponse<WalletAccountExport> excelExportExportExcelCommonResponse = walletAccountService.excelExport(request);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename="+ excelExportExportExcelCommonResponse.getFileName());
        EasyExcel.write(response.getOutputStream(), WalletAccountExport.class).sheet("sheet1").doWrite(excelExportExportExcelCommonResponse.getData());
    }

    @GetMapping("/memberLevel")
    @ApiOperation("会员等级字典")
    public ResponseResult<Map<Integer,Object>>  memberLevel() {
        return success(walletAccountService.memberLevel());
    }

    @GetMapping("/walletType")
    @ApiOperation("钱包类型字典")
    public ResponseResult<Map<Object,Object>>  walletType() {
        return success(walletAccountService.walletType());
    }

}
